package com.xrh.xdemo.service.controllers;

import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.enums.ResultCodeEnum;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.core.utils.XAssert;
import com.xrh.xdemo.api.dto.entity.WordTemplateDTO;
import com.xrh.xdemo.api.request.common.ExportWordRequest;
import com.xrh.xdemo.api.request.queryRequest.WordTemplateQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.WordTemplateSaveRequest;
import com.xrh.xdemo.service.annotations.OperateTrace;
import com.xrh.xdemo.service.aspects.OperateTraceAspect;
import com.xrh.xdemo.service.entity.WordTemplateEntity;
import com.xrh.xdemo.service.service.WordTemplateService;
import com.xrh.xdemo.service.utils.WordUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags="word模板API")
@RestController
@RequestMapping("wordTemplate")
@Slf4j
public class WordTemplateController {

    @Resource
    private WordTemplateService wordTemplateService;

    @Resource
    HttpServletRequest request;
    
    @Resource
    HttpServletResponse response;
    
   	/**
   	 * @function 导出word
   	 * @author 肖荣辉
   	 * @date 2022-09-02 15:07:07
   	 */
   	@ApiOperation("导出word")
   	@PostMapping(value ="/exportWord")
	@OperateTrace(operateType = "导出" , businessType = "word模板" , businessNoKeyPath = "id")
   	public  void exportWord(@RequestBody @Valid  ExportWordRequest exportWordRequest){
   		
   		try {
   			
   			WordTemplateEntity entity = wordTemplateService.findWordTemplateEntityById(exportWordRequest.getId());
   			
   			XAssert.isTrue(entity != null, "id不合法");
   			
   			//替换里面的标签 , 随机替换人的名字
   			entity.setContent(WordUtils.replaceHtmlVarLabelByRandomStr(entity.getContent()));
   			
   			response.setCharacterEncoding("utf-8");
   			
   			String fileName = URLEncoder.encode(entity.getTemplateName() + ".docx", "UTF-8").replaceAll("\\+", "%20");
   			
   			response.setHeader("Content-disposition", "attachment;filename=" + fileName);
   			
   			WordUtils.html2Word(response.getOutputStream() , entity.getContent() == null ? "" : entity.getContent());
   			
   			//设置标题
   			OperateTraceAspect.setTitle(entity.getTemplateName());
   			
   		}  catch (Exception e) {
   			log.error("exportWord 发生错误 ==> " , e);
   		}
   		
   	}
    
   	/**
   	 * @function 删除WordTemplate
   	 * @author 肖荣辉
   	 * @date 2023-12-22 16:10:27
   	*/
   	@ApiOperation("删除WordTemplate")
   	@ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/deleteWordTemplateById")
   	@OperateTrace(operateType = "删除" , businessType = "word模板" , businessNoKeyPath = "id")
   	public CommonResult<Void> deleteWordTemplateById(Long  id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   			
   			wordTemplateService.deleteWordTemplateById(id);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("deleteWordTemplate发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	
   	/**
   	 * @function 保存模板
   	 * @param saveRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-22 16:10:27
   	*/
   	@ApiOperation("保存模板")
   	@PostMapping("/saveWordTemplate")
   	@OperateTrace(operateType = "保存" , businessType = "word模板" ,  businessNoFromReturnResult = true , businessNoKeyPath = "data" , titleKeyPath = "templateName")
   	public  CommonResult<Long> saveWordTemplate(@Valid @RequestBody WordTemplateSaveRequest saveRequest){
   		
   		try {
   			
   			return CommonResult.success(wordTemplateService.saveWordTemplate(saveRequest));
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("saveWordTemplate 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 根据ID获取对象
   	 * @param id
   	 * @author 肖荣辉
   	 * @date 2023-12-22 16:10:27
   	*/
   	@ApiOperation("获取WordTemplate信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/findWordTemplateById")
   	public CommonResult<WordTemplateDTO> findWordTemplateById(Long id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   				
   			WordTemplateDTO dto =  wordTemplateService.findWordTemplateById(id);
   			
   			return CommonResult.success(dto);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findWordTemplateById 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   		
   	}
   	
   	/**
   	 * @function 查询WordTemplate列表
   	 * @author 肖荣辉
   	 * @param wordTemplateQueryRequest
   	 * @date 2023-12-22 16:10:27
   	*/
   	@ApiOperation("获取WordTemplate列表")
   	@PostMapping("/findWordTemplateList")
   	public CommonResult<CommonPage<WordTemplateDTO>> findWordTemplateList(@RequestBody  WordTemplateQueryRequest wordTemplateQueryRequest){
   		
   		try {
   			 
   			return wordTemplateService.findWordTemplateList(wordTemplateQueryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findWordTemplateList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}

}
